package com.ksu.manager.dao.ibatisImpl;

import java.util.List;

import org.mybatis.spring.support.SqlSessionDaoSupport;

import com.ksu.manager.dao.IProductDAO;
import com.ksu.manager.model.Product;

public class ProductDAOImpl extends SqlSessionDaoSupport implements IProductDAO {

	public void create(Product product) {
		try {
			this.getSqlSession().insert("product.insert",product);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	public void del(Product product) {
		try {
			this.getSqlSession().delete("product.delete",product);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	@SuppressWarnings("unchecked")
	public List<Product> queryList(Product product) {
		List<Product> productList = null;
		try {
			if(product==null){
				product = new Product();
			}
			if(product.getName()!=null){
				product.setName("%"+product.getName() + "%");
			}
			productList = (List<Product>)this.getSqlSession().selectList("product.select",product);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return productList;
	}

	public void update(Product product) {
		try {
			this.getSqlSession().insert("product.update",product);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	@SuppressWarnings("unchecked")
	public List<Product> queryListByName(Product product) {
		List<Product> productList = null;
		try {
			productList = (List<Product>)this.getSqlSession().selectList("product.selectRepeat",product);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return productList;
	}

	@Override
	public Product getProduct(Product product) {
		try{
			product = (Product)this.getSqlSession().selectOne("product.selectOne",product);
		}catch(Exception e){
			e.printStackTrace();
		}
		return product;
	}



}
